package com.axxc.jpa.config;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicates;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@ComponentScan(basePackages = { "com.axxc.jpa.web" })
public class SwaggerConfig {

	@PostConstruct
	public void postConstruct() {
		System.err.println("-------------->启用SwaggerUI");
	}
	
	@Value("${server.servlet-path}")
	private String pathMapping;
	
//	@Bean
//	public Docket ProductApi() {
//		return new Docket(DocumentationType.SWAGGER_2)
//				.genericModelSubstitutes(DeferredResult.class)
//				.useDefaultResponseMessages(false)
//				.forCodeGeneration(false)
//				.pathMapping("/v1")
//				.select()
//				.build()
//				.apiInfo(productApiInfo());
//	}

	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2)
				.groupName("base组")
				.pathMapping(pathMapping)//配置访问路径及接口前统一添加该值,即该值会与接口地址拼接@seehttps://www.cnblogs.com/longronglang/p/9045559.html
				.select()// 选择那些路径和api会生成document
				.apis(RequestHandlerSelectors.basePackage("com.axxc.jpa.web"))// 对所有api进行监控
				.paths(Predicates.not(PathSelectors.regex("/error.*")))//排除error
				.build()
				.apiInfo(productApiInfo());
	}
	
    @Bean
    public Docket openApi() {
    	return new Docket(DocumentationType.SWAGGER_2)
    			.groupName("elasticsearch组")
    			.pathMapping(pathMapping)
    			.select()  // 选择那些路径和api会生成document
    			.apis(RequestHandlerSelectors.basePackage("com.axxc.jpa.index.web"))
    			.paths(PathSelectors.any()) // 对所有路径进行监控
    			.build()
    			.apiInfo(productApiInfo());
    }

	private ApiInfo productApiInfo() {
		Contact contact = new Contact("熊骑士", "https://www.axxc.com", "545597162@qq.com");
		return new ApiInfo("spring data jpa前台API接口", // 大标题 title
				"jpa微服务前台API接口", // 小标题
				"1.0.0", // 版本
				"www.axxc.com", // termsOfServiceUrl
				contact, // 作者
				"spring-data-jpa", // 链接显示文字
				"https://www.axxc.com"// 网站链接
		);
	}

}